// pages/my/my.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    defalut_face:'/static/defalut_face.png',
    top: 0, //标题图标距离顶部距离
    opacity: 0,
    scrollTop: 0.5,
    pageIndex: 1,
    loadding: false,
    info:{list:[],uname:'',utel:'',face:''},
    ui:{show_wallet:0,order:[],func:[]},
    pullUpOn: true,
    ver:'',
    wxids:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      ver:`v`+wx.util.ver
    })
    let obj = {};
		obj = wx.getMenuButtonBoundingClientRect();
		wx.getSystemInfo({
			success: res => {
				let width = obj.left || res.windowWidth;
				let height = obj.top ? obj.top + obj.height + 8 : res.statusBarHeight + 44;
				let top = obj.top ? obj.top + (obj.height - 32) / 2 : res.statusBarHeight + 6;
				let scrollH = res.windowWidth * 0.6;

        this.setData({
          top: top,
          height: height,
          width: width,
          scrollH: scrollH
        })
			}
		});
  },

  onShow: function() {
		if(!wx.util.isLogin()){
      this.setData({
        'info.face':''
        ,'info.uname':''
        ,'info.utel':''
      })
		}
		this.load();
	},
  async load(){
    let ui=await wx.util.post("ui/user");
    this.setData({
      ui:ui.data
    })
    let ids=await wx.util.post("msg/get_wx_ids",{type:'worker'});
    this.setData({
      wxids:ids.data
    })
    let res=null;
    try{
      res=await wx.util.post("user/index");
    }catch(e){}
    if(!res){
      return;
    }
    this.setData({
      info:res.data
    })
    
  },
  async switchToWorder(){
    if(!wx.util.isLogin()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    let res=null;
    try{
      res=await wx.util.post("user/switch_worker",{},false);
    }catch(e){}
    if(!res){
      return;
    }
    wx.setStorageSync("sf","worker");
    wx.setStorageSync("worker_token",res.data.token);

    wx.util.showLoading();
    wx.requestSubscribeMessage({
      tmplIds: this.data.wxids,
      complete(e){
        wx.hideLoading();
        wx.redirectTo({
        url:"/pages/workerCode/home"
        })
      }
    })
  },
  toOrder(e){
    let s=e.currentTarget.dataset.index;
    s=s+1;
    if(s==5){
      wx.util.href("/pages/my/refundList",true);
      return;
    }
    wx.setStorageSync("order_tab",s);
    wx.switchTab({
      url:"/pages/myOrder/myOrder"
    })
  },
  async switchToAgent(url){
    if(!wx.util.isLogin()) {
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    await wx.util.post("user/switch_agent",{},false);
    wx.navigateTo({
      url:url
    })
  },
  toPageAc(e){
    let item=e.currentTarget.dataset.item;
    console.log("item",item)
    if(item.type=="kefu"){
      return;
    }
    if(item.type=="link"){
      if(item.url=='/pages/apply'){
        item.url='/pages/apply/apply';
      }
      wx.util.href(item.url,true);
      return;
    }
    if(item.type=="switchToWorder"){
      this.switchToWorder();
      return;
    }
    if(item.type=="switchToAgent"){
      this.switchToAgent(item.url);
      return;
    }
  },
  toPage(e){
    let page=e.currentTarget.dataset.page;
    wx.util.href("/pages/"+page,true);
  },
  detail: function(id) {
    wx.navigateTo({
      url: '../productDetail/productDetail?id='+id
    });
  },
  initNavigation(e) {
    this.setData({
      top: e.top,
      opacity: e.opacity
    })
  },
  opacityChange(e) {
    this.setData({
      opacity: e.opacity
    })
  },
  back() {
    wx.navigateBack();
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
	},
	onPullDownRefresh() {
		setTimeout(() => {
			wx.stopPullDownRefresh();
		}, 200);
	}

})